<?php

namespace app\hhlock\controller;

use support\Request;

class IndexController
{
    public function index(Request $request)
    {
        return response(__CLASS__);
    }
    public function remote_open(Request $request){
        $lock_no = $request->post('lock_no','');
        if(!$lock_no){
            return json(['code'=>0,'msg'=>'缺失参数lock_no']);
        }
        $account = $request->post('account','');
        if(!$account){
            return json(['code'=>0,'msg'=>'缺失参数account']);
        }
        $password = $request->post('password','');
        if(!$password){
            return json(['code'=>0,'msg'=>'缺失参数password']);
        }
        $key = $request->post('key','');
        if(!$key){
            return json(['code'=>0,'msg'=>'缺失参数key']);
        }
        $res = $this->huohelogin($account,$password,$key);
        if(isset($res['data']['access_token'])){
            $token = $res['data']['access_token'];
        }else{
            return json($res);
        }
        
        $url = 'http://ops.huohetech.com/lock/remote_open';
        $headers['version'] = '1.0';
        $headers['s_id'] = time().rand(10000,99999);
        $headers['access_token'] = $token;
        $data['lock_no'] = $lock_no;
        $data['pwd_user_mobile'] = $account;
        $res = $this->PostJson($url,$data,$headers);
        return json($res);
    }
    //门锁授权
    public function lockauth(Request $request){
        $lock_no = $request->post('lock_no','');
        if(!$lock_no){
            return json(['code'=>0,'msg'=>'缺失参数lock_no']);
        }
        $account = $request->post('account','');
        if(!$account){
            return json(['code'=>0,'msg'=>'缺失参数account']);
        }
        $password = $request->post('password','');
        if(!$password){
            return json(['code'=>0,'msg'=>'缺失参数password']);
        }
        $key = $request->post('key','');
        if(!$key){
            return json(['code'=>0,'msg'=>'缺失参数key']);
        }
        $res = $this->huohelogin($account,$password,$key);
        if(isset($res['data']['access_token'])){
            $token = $res['data']['access_token'];
        }else{
            return json($res);
        }
        $url = 'http://ops.huohetech.com/lock/auth';
        $headers['version'] = '1.0';
        $headers['s_id'] = time().rand(10000,99999);
        $headers['access_token'] = $token;
        $data['lock_no'] = $lock_no;
        $data['mobile'] = $account;
        $data['set_custom_pwd'] ='Y';
        $data['auth_time_start'] =time()*1000;
        $data['auth_time_end'] =(time()+360*24*60*60)*1000;
        $res = $this->PostJson($url,$data,$headers);
        return json($res);
    }
    //门锁解除授权
    public function cancel_auth(Request $request){
        $lock_no = $request->post('lock_no','');
        if(!$lock_no){
            return json(['code'=>0,'msg'=>'缺失参数lock_no']);
        }
        $account = $request->post('account','');
        if(!$account){
            return json(['code'=>0,'msg'=>'缺失参数account']);
        }
        $password = $request->post('password','');
        if(!$password){
            return json(['code'=>0,'msg'=>'缺失参数password']);
        }
        $key = $request->post('key','');
        if(!$key){
            return json(['code'=>0,'msg'=>'缺失参数key']);
        }
        $res = $this->huohelogin($account,$password,$key);
        if(isset($res['data']['access_token'])){
            $token = $res['data']['access_token'];
        }else{
            return json($res);
        }
        $url = 'http://ops.huohetech.com/lock/cancel_auth';
        $headers['version'] = '1.0';
        $headers['s_id'] = time().rand(10000,99999);
        $headers['access_token'] = $token;
        $data['lock_no'] = $lock_no;
        $data['mobile'] = $account;
        $res = $this->PostJson($url,$data,$headers);
        return json($res);
    }
    //火河登录
    public function huohelogin($account,$password,$key){
        $url = 'http://ops.huohetech.com/login';
        $headers['version'] = '1.0';
        $headers['s_id'] = time().rand(10000,99999);
        $data['account'] = $account;
        $data['password'] = $this->setdecpass($key,$password);
        $res = $this->PostJson($url,$data,$headers);
        return $res;
    }
    public function setdecpass($key,$data){
        // $key = "wgjNumKR"; // DES密钥应为8字节长度
        $method = "DES-ECB"; // CBC模式
        $iv = '';
        $encrypted = openssl_encrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv);
        return  bin2hex($encrypted);
    }
    //发送json请求函数
    public function PostJson($url, $data = NULL,$headers=[])
    {
        $curl = curl_init();
        $header =array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen(json_encode($data)),
            'Cache-Control: no-cache',
            'Pragma: no-cache'
        );
        if($headers){
            foreach($headers as $k=>$v){
                $header[] = $k.':'.$v;
            }
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!$data) {
            return 'data is null';
        }
        if (is_array($data)) {
            $data = json_encode($data);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return $errorno;
        }
        curl_close($curl);
        return json_decode($res,true);
    }
}
